package jp.seraph.same.core;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;

import org.apache.commons.io.FileUtils;

import jp.seraph.jsade.core.DefaultAgentRunner;
import jp.seraph.jsade.core.DefaultWorld;
import jp.seraph.jsade.core.NopSeeObjectCalculator;
import jp.seraph.jsade.model.nao.NaoJointIdentifierContainer;
import jp.seraph.jsade.model.nao.NaoModelManager;
import jp.seraph.jsade.model.nao.NaoObjectContainer;
import jp.seraph.jsade.task.TaskSelectorAgent;
import jp.seraph.jspf.codec.FilePoseManager;
import jp.seraph.jspf.codec.JsonPoseCodec;
import jp.seraph.jspf.pose.DefaultPose;
import jp.seraph.jspf.utils.NaoSymmetricJointContext;
import jp.seraph.jspf.utils.PoseUtil;
import jp.seraph.same.controller.DefaultPoseController;
import jp.seraph.same.model.DefaultPoseCollectionModel;
import jp.seraph.same.model.NaoJointPositionMapper;
import jp.seraph.same.model.PoseCollectionModel;
import jp.seraph.same.view.PosePanel;

public class PosePanelRunner {

    /**
     * @param args
     */
    public static void main(String[] args) {

        EditorMotionSelector tSelector = new EditorMotionSelector();
        TaskSelectorAgent tAgent = new TaskSelectorAgent(tSelector);
        final DefaultAgentRunner tRunner = new DefaultAgentRunner(new DefaultWorld(new NaoModelManager(0), new NopSeeObjectCalculator()), new SampleAgentContext(), tAgent);

        tRunner.start();
        File tTestDir = new File("test");

        PoseCollectionModel tModel = new DefaultPoseCollectionModel(new FilePoseManager(tTestDir, new JsonPoseCodec(new NaoJointIdentifierContainer()), true), new NaoObjectContainer(), new NaoJointIdentifierContainer(), new NaoJointPositionMapper());
        DefaultPoseController tController = new DefaultPoseController(tModel, tSelector, new PoseUtil(new NaoSymmetricJointContext()), new DefaultPose(new NaoJointIdentifierContainer()));

        PosePanel tPanel = new PosePanel(tModel, tController);
        JFrame tFrame = new JFrame("JointPanel");
        tFrame.add(tPanel);
        tFrame.setSize(600, 400);

        tFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        tFrame.addWindowListener(new WindowListener() {

            public void windowActivated(WindowEvent aE) {
            }

            public void windowClosed(WindowEvent aE) {
                tRunner.stopAgent();
            }

            public void windowClosing(WindowEvent aE) {
            }

            public void windowDeactivated(WindowEvent aE) {
            }

            public void windowDeiconified(WindowEvent aE) {
            }

            public void windowIconified(WindowEvent aE) {
            }

            public void windowOpened(WindowEvent aE) {
            }

        });
        tFrame.setVisible(true);

        try {
            tRunner.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (tTestDir.exists())
            try {
                FileUtils.deleteDirectory(tTestDir);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}
